package com.example.bai12;

import java.util.ArrayList;

import com.example.adapter.NhanVienAdapter;
import com.example.object.ChucVu;
import com.example.object.NhanVien;
import com.example.object.PhongBan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;

public class ThietLapLanhDaoActivity extends Activity{
	
	
	ListView listtp,listpp;
	ArrayList<NhanVien> arrtp,arrpp;
	NhanVienAdapter adaptertp,adapterpp;
	ImageButton btnok;
	PhongBan pb=null;
	NhanVien tp=null;
	ArrayList<NhanVien> pp=new ArrayList<NhanVien>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.thiet_lap_lanh_dao);
		getControls();
		addEvent();
	}
	private void getControls()
	{
		btnok=(ImageButton) findViewById(R.id.btnok);
		
		listtp=(ListView) findViewById(R.id.listtp);
		listtp.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		
		listpp= (ListView) findViewById(R.id.listpp);
		listpp.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		
		Intent i=getIntent();
		Bundle b= i.getBundleExtra("DATA");
		 pb= (PhongBan) b.getSerializable("PHONGBAN");
		arrpp=pb.getList();
		arrtp=pb.getList();
		
		adapterpp= new NhanVienAdapter(this,android.R.layout.simple_list_item_1,arrpp);
		listpp.setAdapter(adapterpp);
		
		adaptertp= new NhanVienAdapter(this, android.R.layout.simple_list_item_1,arrtp);
		listtp.setAdapter(adaptertp);
	}
	private void addEvent()
	{
		btnok.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(tp!=null)
				{
					tp.setChucvu(ChucVu.TruongPhong);
				}
				if(pp.size()>0)
				{
					for(NhanVien i:pp)
					{
						i.setChucvu(ChucVu.PhoPhong);
					}
				}
				Intent i= getIntent();
				Bundle b= new Bundle();
				b.putSerializable("PHONGBAN", pb);
				i.putExtra("DATA", b);
				setResult(MainActivity.THIET_LAP_TP_PP_THANH_CONG, i);
				finish();
				
			}
		});
		listpp.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				NhanVien nv=arrpp.get(arg2);
				int i;
				for( i=0;i<pp.size();i++)
				{
					if(arrpp.get(i).getId().equalsIgnoreCase(nv.getId()))
					{
						break;
					}
				}
				if(pp.size()>0 &&i<=pp.size())
				{
						pp.remove(i);
						return;
				}
				pp.add(nv);
			}
		});
		listtp.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				tp=arrtp.get(arg2);
			}
		});
	}
}
